package com.jiaai.util.JPush;


import cn.jiguang.common.ClientConfig;
import cn.jiguang.common.resp.APIConnectionException;
import cn.jiguang.common.resp.APIRequestException;
import cn.jpush.api.JPushClient;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.Message;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.notification.Notification;
import com.jiaai.util.ThreadPoolProxy;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * class_name:JPushUtil
 * describe:TODO
 * create_user:张昉平
 * create_date:2018/1/8
 * create_date:17:16
 */
public class JPushUtil {
    private static  final Logger log= LoggerFactory.getLogger(JPushUtil.class);
    public static final int ALIAS = 1;//别名
    public static final int TAG = 2;//标签
    public static final int ALL = 5;//所有

    public static final int NOTIFY = 3;//通知栏
    public static final int UNNOTIFY = 4;//透传

    private static JPushUtil jPushUtil;

    private static JPushClient jPushClient;
    private static ThreadPoolProxy poolProxy;


    public static  synchronized   JPushUtil getInstance(){
            if(jPushUtil==null){
                jPushUtil=new JPushUtil();
            }
            return jPushUtil;
    }
    public  void init(String ak, String as,ThreadPoolProxy threadPoolProxy){
        if(jPushClient!=null){
            log.warn("极光推送重复初始化");
            return;
        }

        poolProxy=threadPoolProxy;
        ClientConfig config = ClientConfig.getInstance();
        config.setMaxRetryTimes(5);
        config.setConnectionTimeout(10 * 1000); // 10 seconds
        config.setSSLVersion("TLSv1.1");        // JPush server supports SSLv3, TLSv1, TLSv1.1, TLSv1.2
        jPushClient = new JPushClient(ak,as, null, config);
    }


    public  PushPayload buildPayloadMsg(String msg, int audienceFlag, int notiFlag, String... targets){
        if(jPushClient==null){
            log.error("jpushutil is not init");
            return null;
        }
        PushPayload.Builder builder = PushPayload.newBuilder()
                .setPlatform(Platform.all());
        if (audienceFlag == ALIAS) builder.setAudience(Audience.alias(targets));//别名
        else if (audienceFlag == TAG) builder.setAudience(Audience.tag(targets));//标签
        else if (audienceFlag == ALL) builder.setAudience(Audience.all());//所有人

        if (notiFlag == NOTIFY) builder.setNotification(Notification.alert(msg));//通知栏
        else if (notiFlag == UNNOTIFY) builder.setMessage(Message.content(msg));//透传

        return builder.build();
    }

    public  PushResult sendPush(PushPayload pushPayload){
        if(jPushClient==null){
            log.error("jpushutil is not init");
            return null;
        }
        try {

            PushResult res = jPushClient.sendPush(pushPayload);
            return res;
        } catch (APIConnectionException e) {
            e.printStackTrace();
        } catch (APIRequestException e) {
            e.printStackTrace();
        } catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }
    /**
     * 推送消息给所有平台匹配标签的用户
     * @param title
     * @param msg
     * @param alias
     */
    public void pushMsgByAllByAlias(String title, Message msg,String ... alias){
        poolProxy.commitTask(new JPushThread(jPushClient,buildPushObject_all_alias(title,msg,alias)));
    }
    /**
     * 构建所有平台按标签的结构体
     * @return
     */
    private PushPayload buildPushObject_all_alias(String title, Message msg, String ...alias) {
        return PushPayload.newBuilder()
                .setPlatform(Platform.all())
                .setAudience(Audience.alias(alias))
                .setNotification(Notification.alert(title))
                .setMessage(msg)
                .build();

    }
//    public static void main(String[] args) {
//        PushPayload p = buildPayloadMsg("你大爷的", JPushUtil.ALL, JPushUtil.NOTIFY, "232");
//        System.out.println();
//        System.out.println(sendPush(p, "73a653e0914fff9baa851cac", "1271b55e0181e9d09feba5db" ));
//    }
}
